package com.share.PACManager.task;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.share.PACManager.model.PoliceModel;
import com.share.PACManager.model.TerminalModel;
import com.share.PACManager.net.HTTPResponse;
import com.share.PACManager.net.HttpUtility;
import com.share.PACManager.utils.Cryptography;
import com.share.PACManager.utils.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;


public class GetViewTask extends Thread{

	private GetViewListener mListener;
	private Context mContext;	
	private TerminalModel mTerminal;
	private List<PoliceModel> mPolices;
	private String mIndex;
	private String mUrl;
	
	public interface GetViewListener {
		public TerminalModel requestTerminal();
		public String requestIndex();
		public void onCheckResult(boolean result, String url);
	}

	public GetViewTask(Context context) {
		mContext = context;
	}
	
	public void start(GetViewListener listener) {
		mListener = listener;
		if(mListener != null)
		{
			mTerminal = mListener.requestTerminal();
			mIndex = mListener.requestIndex();
			
			start();
		}
	}

	private Handler handler = new Handler(){		
		@Override
		public void handleMessage(Message msg)
		{
			if(mListener != null)
			{
				if(msg.what != 0)
					mListener.onCheckResult(false, mUrl);
				else
				{
					HTTPResponse resp = (HTTPResponse) msg.obj;
					mListener.onCheckResult(parse(resp.content), mUrl);								
				}
			}
		}
	};
	
    public boolean parse(String json)
    {
		try {
			JSONObject jsonObject = new JSONObject(json);
			if(!jsonObject.getBoolean("Success"))
				return false;			
			String url = jsonObject.getString("State");
			if(!Utils.isEmptyString(url))
			{
				mUrl = url;
				return true;
			}			
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return false;
    }
    
	@Override
	public void run() {

		try {

			String terminalId = mTerminal.TerminalId;
			String imei = mTerminal.TerminalCode;
			String password = mTerminal.TerminalPassword;
			String ticket = Cryptography.AES_Encode(imei, terminalId, password);
			
			Map<String, String> params = new TreeMap<String, String>();
			params.put("Id", terminalId);
			params.put("TerminalId", terminalId);
			params.put("DataType", "Data");	
			params.put("Password", password);
			params.put("TerminalCode", imei);
			params.put("Version", "1.0");
			params.put("Ticket", ticket);
			params.put("Objective", "GetView");
			
			String info = Cryptography.AES_Encode("Controller="+mIndex, terminalId, password);
			String entity = "Information=" + URLEncoder.encode(info);	
					
			handler.obtainMessage(0, HttpUtility.httpPost(params, entity)).sendToTarget();

		} catch (Exception e) {
			handler.obtainMessage(1).sendToTarget();
		}
		
		super.run();
	}
}
